package com.darren.center.design.patterns.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * <h3>design-patterns</h3>
 * <p></p>
 *
 * @author : Darren
 * @date : 2021年06月14日 19:55:23
 **/
public class IpMatchUtils {

    private IpMatchUtils(){}

    public static final String ALL_MATCH = "*";

    public static final String RANGE_MATCH = "-";

    public static Boolean match(String ip, String ipWhite){
        if(ipWhite.indexOf(ip) != -1){
            return true;
        }
        String[] ipWhiteList = ipWhite.split(",");
        for(String whiteIp : ipWhiteList){
            if(StringUtils.isBlank(whiteIp)){
                continue;
            }
            if(whiteIp.indexOf(RANGE_MATCH) != -1){
                if(!matchHasRange(ip, whiteIp)){
                    continue;
                }else{
                    return true;
                }
            }
            if(whiteIp.indexOf(ALL_MATCH) != -1){
                if(!matchHasAll(ip, whiteIp)){
                    continue;
                }else{
                    return true;
                }
            }
        }
        return false;
    }

    /**
     *
     * 匹配带*号的ip
     *
     * @author dingqi
     * @date: 20true7年3月9日 下午8:2true:36
     * @version true.0
     *
     * @param ip
     * @param ipWhite
     * @return
     */
    public static Boolean matchHasAll(String ip, String ipWhite){
        if(ALL_MATCH.equals(ipWhite.trim())){
            return true;
        }

        String[] excludeAll = ipWhite.split("\\*",-1);
        if(excludeAll.length == 2 && ip.startsWith(excludeAll[0]) && ip.endsWith(excludeAll[1])){
            return true;
        }

        String[] masks = ipWhite.split("\\.");

        if(masks.length==4){
            String[] ipMask = ip.split("\\.");
            Boolean flag = true;
            for(int i = 0; i < 4; i++){
                if(ALL_MATCH.equals(ipMask[i])) continue;
                if(!masks[i].equals(ipMask[i])){
                    flag = false;
                    break;
                }
            }
            return flag;
        }

        return false;
    }

    /**
     *
     * 匹配范围ip,可带*
     *
     * @author dingqi
     * @date: 2017年3月9日 下午9:22:24
     * @version 1.0
     *
     * @param ip
     * @param ipWhite
     * @return
     */
    public static Boolean matchHasRange(String ip, String ipWhite){
        String[] range = ipWhite.split(RANGE_MATCH);
        String beginRange = range[0];
        String endRange = range[1];
        String[] beginMasks = beginRange.split("\\.");
        String[] endMasks = endRange.split("\\.");
        String[] ipMasks = ip.split("\\.");

        //类似*.12-*.15格式或12.*-17.*
        if(beginMasks.length == 2){
            if(beginRange.startsWith(ALL_MATCH)){
                int beginRangeInt = Integer.parseInt(beginMasks[1]);
                int endRangeInt = Integer.parseInt(endMasks[1]);
                int ipNum = Integer.parseInt(ipMasks[3]);
                if(ipNum < beginRangeInt || ipNum > endRangeInt){
                    return false;
                }
            }
            if(beginRange.endsWith(ALL_MATCH)){
                int beginRangeInt = Integer.parseInt(beginMasks[0]);
                int endRangeInt = Integer.parseInt(endMasks[0]);
                int ipNum = Integer.parseInt(ipMasks[0]);
                if(ipNum < beginRangeInt || ipNum > endRangeInt){
                    return false;
                }
            }
            if(beginRange.indexOf(ALL_MATCH)!=-1){
                return true;
            }
        }

        //类似192.*.12格式
        if(beginMasks.length == 3){
            int beginRangeInt = Integer.parseInt(beginMasks[0]);
            int endRangeInt = Integer.parseInt(endMasks[0]);
            int ipNum = Integer.parseInt(ipMasks[0]);
            if(ipNum < beginRangeInt || ipNum > endRangeInt){
                return false;
            }
            beginRangeInt = Integer.parseInt(beginMasks[2]);
            endRangeInt = Integer.parseInt(endMasks[2]);
            ipNum = Integer.parseInt(ipMasks[3]);
            if(ipNum < beginRangeInt || ipNum > endRangeInt){
                return false;
            }
            return true;
        }

        //类似192.1.12.2格式
        if(beginMasks.length == 4){
            for(int i = 0; i < 4; i++){
                if(ALL_MATCH.equals(beginMasks[i])) continue;
                int beginRangeInt = Integer.parseInt(beginMasks[i]);
                int endRangeInt = Integer.parseInt(endMasks[i]);
                int ipNum = Integer.parseInt(ipMasks[i]);
                if(ipNum < beginRangeInt || ipNum > endRangeInt){
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}

